new Vue({
	el: "body",
	data: {
		shopmain: [], //商家
		shoplist: [], //分类
		foodlist: [], //商品列表
		shop: true,
		evaluate: false,
		total: 0,

	},
	methods: {
		shoper: function() {
			//跳转到商家详情页
			location.href = "../shoper/shoper.html?id=" + geturlitem(location.search, "shopid");
		},
		active: function(index) {
			$(".shoplist ul li").removeClass("shopactive");
			$(".shoplist ul li").eq(index).addClass("shopactive");
		},
		shope: function() {
			this.shop = true;
			this.evaluate = false;
		},
		evaluater: function() {
			this.shop = false;
			this.evaluate = true;
		},
		addpro: function(si) {
			for(var i = 0; i < this.foodlist.length; i++) {
				if(this.foodlist[i].foodid == si) {
					this.foodlist[i].productnum += 1;
					this.total += this.foodlist[i].price * 1;
				}
			}

		},
		reduce: function(si) {
			for(var i = 0; i < this.foodlist.length; i++) {
				if(this.foodlist[i].foodid == si) {
					this.foodlist[i].productnum -= 1;
					this.total -= this.foodlist[i].price * 1;
				}
			}
		},
		Settlement: function() {
			if(this.total > 0) {
				var currentUser = AV.User.current();
				if(currentUser) {
					// 跳转到结算页
					var shop_list = [];
					for(var i = 0; i < this.foodlist.length; i++) {
						if(this.foodlist[i].productnum > 0) {
							shop_list.push(this.foodlist[i]);
						}
					}
					var shop_name=this.shopmain[0].shopname;//商店名
					var shopimg=this.shopmain[0].shopimg;//商店图
					var total = this.total;//总价
					var if_pay=false;//是否支付
					var shoplist=shop_list;//商品列表
					var username=currentUser.attributes.username;//name
					var shop_sendprice=this.shopmain[0].distribution;//配送费
					console.log(shop_sendprice);

					var Product = AV.Object.extend('ELM_order');
					var product = new Product();
					product.set('if_pay',if_pay);//是否支付
					product.set('shopimg',shopimg);//图片
					product.set('shop_sendprice',shop_sendprice);//配送费
					product.set('username',username);//用户名
					product.set('total',total);//总价
					product.set('products_data',shoplist);//商品列表
					product.set('shop_name',shop_name);//商店名
					
					product.save().then(function(success) {
						//  发布成功
						location.href="../confirmOrder/confirmOrder.html?orderid="+success.id;
					}, function(error) {
						alert("添加失败");
						alert(JSON.stringify(error));
					});
				} else {
					//currentUser 为空时，可打开用户注册界面…
					location.href = "../loginAndReg/login.html" + location.search;
				}
			}
		}

	},
	created: function() {
		var this_shopmain = this.shopmain;
		var this_shopname;
		var this_notice; //公告
		var this_shopimg;
		var this_destribution;//配送费
		init();
		var myurl = location.search;
		var shopid = geturlitem(myurl, "shopid");
		var query = new AV.Query('ELM_shop'); //商家表
		query.get(shopid).then(function(shoplist) {
			// 查询到商品后，在前端展示到相应的位置中
			this_shopname = shoplist.attributes.shopname;
			this_notice = shoplist.attributes.notice;
			this_shopimg = shoplist.attributes.shopimg;
			this_distribution = shoplist.attributes.sendprice;
			var a = {
				shopname: this_shopname,
				notice: this_notice,
				shopimg: this_shopimg,
				distribution:this_distribution
			}
			this_shopmain.push(a);
		}).catch(function(error) {
			console.log(JSON.stringify(error));
		});
		//商品表
		var shop_list = this.shoplist;
		var query1 = new AV.Query("ELM_product");
		var tar = query1.contains("shopid", shopid);
		var arr = [];
		var food_list = this.foodlist;
		tar.find().then(function(item) {
			for(var i = 0; i < item.length; i++) {
				var classify = item[i].attributes.classify.split(",");
				for(var j = 0; j < classify.length; j++) {
					if(arr.indexOf(classify[j]) < 0) {
						arr.push(classify[j]); //店家商品的所有分类
					}
				}
			}
			shop_list.push(arr);
		})

		tar.find().then(function(item) {
			console.log(item);
			for(var j = 0; j < item.length; j++) {
				var food = item[j].attributes;
				var foodlists = {
					classify: food.classify, //所属类别
					productname: food.productname, //商品名称
					remark: food.remark, //注释
					shopid: food.shopid, //商家id
					price: food.price, //价格
					salenum: food.salenum, //月销量
					grade: food.grade, //好评率
					productnum: 0, //个数
					foodid: item[j].id,
					productimg:food.productimg
				};
				food_list.push(foodlists);
			}
		})
	}
})